<?php

namespace Managers;

class Projects extends BaseManager
{
    protected $__table = '\Catalog\Projects';

    public function getList($limit, $start = 0, $public = 1)
    {
        $qb = $this->_em->createQueryBuilder()
                ->select('projects')
                ->from($this->__table, 'projects');
        if (isset($public)) {
            $qb = $qb->where("projects.public= :public")->setParameter('public', $public);
        }

        $projects = $qb->orderBy("projects.date", "desc")
                ->setFirstResult($start)
                ->setMaxResults($limit)
                ->getQuery()
                ->getArrayResult();
        return $projects;
    }

    public  function  getOne($id)
    {
       $res = $this->_em->createQueryBuilder()
                ->select('p')
                ->from($this->__table, 'p')
                ->where("p.id= :id")->setParameter('id', $id)
                ->getQuery()
                ->getArrayResult();
        return $res;
    }

    public function getTotalRecords()
    {
        $res = $this->_em->createQueryBuilder()
                ->select('count (p.id) as total')
                ->from($this->__table, 'p')
                ->getQuery()
                ->getArrayResult();

        return $res[0]['total'];
    }

//    /**
//     * @throws Exception
//     * @param $name
//     * @param $des_a_img_url
//     * @param $file
//     * @param $d_x
//     * @return void
//     * @author b179a9
//     */
    function resizer($name, $des_a_img_url, $file, $d_x)
    {
        if (!extension_loaded('imagick')) {
            throw new \Exception('Image magick php is not installed.');
        }
        try {
            $image = new \Imagick($file);
            $image->thumbnailImage($d_x, null);
            $fd = fopen($des_a_img_url, 'w');
            $image->writeImageFile($fd);
            fclose($fd);
            unlink($file);
        } catch (\ImagickException $e) {
            throw new \Exception('Unsupported file format.');
        }
    }

    public function update($id, $title, $desc, $pub)
    {
        $object = $this->_em->find($this->__table, $id);
        if ($object) {
            $object->setTitle($title);
            $object->setContent($desc);
            $object->setPublic($pub);
            $this->_em->persist($object);
            $this->_em->flush();

            return $object;
        }
        return false;
    }

    public function add($title, $desc,  $pub)
    {
        $object = new \Catalog\Projects();
        $object->setTitle($title);
        $object->setContent($desc);
        $object->setPublic($pub);
        $object->setDate(new \DateTime());
        $object->setViews(0);

        $this->_em->persist($object);
        $this->_em->flush();

        return $object;
    }
//
    function setPosted($id)
    {
        $object = $this->_em->find($this->__table, $id);
        if ($object) {
            $object->setPublic(1);

            $this->_em->persist($object);
            $this->_em->flush();
            return true;
        }
        return false;
    }

    function setImage($id, $img) {
        $object = $this->_em->find($this->__table, $id);
        if ($object) {
            $object->setImg($img);

            $this->_em->persist($object);
            $this->_em->flush();
            return $object;
        }
        return false;
    }

}